package cn.mado.org.service.impl;

import cn.mado.basic.util.PageList;
import cn.mado.org.domain.Writer;
import cn.mado.org.mapper.WriterMapper;
import cn.mado.org.query.WriterQuery;
import cn.mado.org.service.WriterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class WriterServiceImpl implements WriterService {

    @Autowired
    private WriterMapper writerMapper;
    //删除
    @Override
    @Transactional
    public void deleteWriterById(Long writerId) {
        writerMapper.deleteWriterById(writerId);
    }
    //新增
    @Override
    @Transactional
    public void addWriter(Writer writer) {
        //创建时间
        writer.setCreatetime(new Date());
        writerMapper.addWriter(writer);
    }
    //修改
    @Override
    @Transactional
    public void updateWriter(Writer writer) {
        writerMapper.updateWriter(writer);
    }
    //查询
    @Override
    public List<Writer> findAll() {
        return writerMapper.findAll();
    }
    //单条查询
    @Override
    public Writer findWriterById(Long writerId) {
        return writerMapper.findWriterById(writerId);
    }
    //分页查询
    @Override
    public PageList<Writer> queryByPage(WriterQuery writerQuery) {
        //查询总条数
        Integer totals = writerMapper.queryByCount(writerQuery);
        if (null == totals || 0 == totals){
            return new PageList<>(0,new ArrayList<>());
        }
        //查询数据
        List<Writer> writers = writerMapper.queryByPage(writerQuery);

        //返回
        return new PageList<>(totals,writers);
    }


}
